package com.example.xupdateLearningProject.custom;

import com.example.xupdateLearningProject.entity.CustomResult;
import com.xuexiang.xupdate.entity.UpdateEntity;
import com.xuexiang.xupdate.listener.IUpdateParseCallback;
import com.xuexiang.xupdate.proxy.IUpdateParser;
import com.xuexiang.xutil.net.JsonUtil;

/**
 * 自定义更新解析器
 */
public class CustomUpdateParser implements IUpdateParser {
    @Override
    public UpdateEntity parseJson(String json) throws Exception {
        return getParseResult(json);
    }

    @Override
    public void parseJson(String json, IUpdateParseCallback callback) throws Exception {
        callback.onParseResult(getParseResult(json));
    }

    @Override
    public boolean isAsyncParser() {
        return false;
    }

    private UpdateEntity getParseResult(String json){
        CustomResult result = JsonUtil.fromJson(json,CustomResult.class);
        if (result != null) {
            return new UpdateEntity()
                    .setHasUpdate(result.hasUpdate)
                    .setIsIgnorable(result.isIgnorable)
                    .setVersionCode(result.versionCode)
                    .setVersionName(result.versionName)
                    .setDownloadUrl(result.apkUrl)
                    .setUpdateContent(result.updateLog)
                    .setSize(result.apkSize);
        }
        return null;
    }
}
